package framework.editor;


import framework.data.Pessoa;
import framework.data.Produto;
import framework.db.PessoaManager;
import framework.panes.Editor;
import framework.panes.Editor;
import framework.panes.EditorPane;
import framework.panes.EditorPane;
import java.awt.Frame;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ProdutoPane.java
 *
 * Created on May 5, 2011, 3:24:19 PM
 */
/**
 *
 * @author usuario
 */
public class ProdutoEditor extends EditorPane
{
    private Produto produto;

    /**
     * 
     * @param Parent
     * @param produto
     * @return 
     */
    public static boolean edit( Frame parent, Produto produto )
    {
        ProdutoEditor pane = new ProdutoEditor();
        
        pane.setProduto( produto );
        
        return Editor.edit( parent, pane, "Cadastro de produto" );
    }
    
    /** Creates new form ProdutoPane */
    public ProdutoEditor()
    {
        initComponents ();
    }
    
    /**
     * 
     * @param p 
     */
    private void setProduto( Produto p )
    {
        this.produto = p;
        preencherCampos();
    }
    
    /**
     * 
     */
    private void preencherCampos()
    {
        tfCodigo.setText( String.valueOf( produto.getCodigo() ) );
        tfAlternativo.setText( produto.getAlternativo() );
        tfDescricao.setText( produto.getDescricao() );
        tfEstruturado.setText( produto.getEstruturado() );
        tfFornecedor.setText( String.valueOf( produto.getFornecedor() ) );
        tfImposto.setText( String.valueOf( produto.getImposto() ) );
        tfMensagem.setText( String.valueOf( produto.getMensagem() ) );
        tfNcm.setText( String.valueOf( produto.getNcm() ) );
    }
    
    /**
     * 
     */
    @Override
    public void accept()
    {
        produto.setCodigo( Integer.parseInt( tfCodigo.getText() ) );
        produto.setAlternativo( tfAlternativo.getText() );
        produto.setDescricao( tfDescricao.getText() );
        produto.setEstruturado( tfEstruturado.getText() );
        produto.setFornecedor( Integer.parseInt( tfFornecedor.getText() ) );       
        produto.setImposto( Integer.parseInt( tfImposto.getText() ) );
        produto.setMensagem( Integer.parseInt( tfMensagem.getText() ) );
        produto.setNcm( Integer.parseInt( tfNcm.getText() ) );
    }

    @Override
    public String validateInput()
    {
        String erro = "";
        
        if ( tfDescImposto.getText().isEmpty())
        {
            erro = "\n - Tipo de imposto inválido.";
        }
        if ( !tfFornecedor.getText().isEmpty() && tfNomeFornecedor.getText().isEmpty())
        {
            erro += "\n - Fornecedor inválido.";
        }            
        if ( !tfMensagem.getText().isEmpty() && tfDescMensagem.getText().isEmpty())
        {
            erro += "\n - Mensagem inválida.";
        }            
        return erro;
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        cbInativo = new javax.swing.JCheckBox();
        tfDescricao = new javax.swing.JTextField();
        tfNcm = new javax.swing.JTextField();
        tfCodigo = new javax.swing.JTextField();
        tfEstruturado = new javax.swing.JTextField();
        tfAlternativo = new javax.swing.JTextField();
        jPanel4 = new javax.swing.JPanel();
        tfImposto = new javax.swing.JTextField();
        tfDescImposto = new javax.swing.JTextField();
        jPanel5 = new javax.swing.JPanel();
        tfMensagem = new javax.swing.JTextField();
        tfDescMensagem = new javax.swing.JTextField();
        jPanel6 = new javax.swing.JPanel();
        tfFornecedor = new javax.swing.JTextField();
        tfNomeFornecedor = new javax.swing.JTextField();

        setLayout(new java.awt.GridBagLayout());

        jLabel1.setText("Produto:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 15, 5, 5);
        add(jLabel1, gridBagConstraints);

        jLabel2.setText("Estruturado:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 15, 5, 5);
        add(jLabel2, gridBagConstraints);

        jLabel3.setText("Alternativo:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 5);
        add(jLabel3, gridBagConstraints);

        jLabel5.setText("Tipo imposto:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 15, 5, 5);
        add(jLabel5, gridBagConstraints);

        jLabel6.setText("NCM:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 15, 5, 5);
        add(jLabel6, gridBagConstraints);

        jLabel7.setText("Mensagem:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 15, 5, 5);
        add(jLabel7, gridBagConstraints);

        jLabel8.setText("Fornecedor:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 15, 15, 5);
        add(jLabel8, gridBagConstraints);

        cbInativo.setText("Inativo");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 15, 15);
        add(cbInativo, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 15);
        add(tfDescricao, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 15);
        add(tfNcm, gridBagConstraints);

        tfCodigo.setEditable(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(15, 0, 5, 15);
        add(tfCodigo, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 5);
        add(tfEstruturado, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 15);
        add(tfAlternativo, gridBagConstraints);

        jPanel4.setLayout(new java.awt.GridBagLayout());

        tfImposto.setPreferredSize(new java.awt.Dimension(60, 20));
        tfImposto.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                tfImpostoFocusLost(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 5);
        jPanel4.add(tfImposto, gridBagConstraints);

        tfDescImposto.setEditable(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 15);
        jPanel4.add(tfDescImposto, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        add(jPanel4, gridBagConstraints);

        jPanel5.setLayout(new java.awt.GridBagLayout());

        tfMensagem.setPreferredSize(new java.awt.Dimension(60, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 5);
        jPanel5.add(tfMensagem, gridBagConstraints);

        tfDescMensagem.setEditable(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 15);
        jPanel5.add(tfDescMensagem, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        add(jPanel5, gridBagConstraints);

        jPanel6.setLayout(new java.awt.GridBagLayout());

        tfFornecedor.setPreferredSize(new java.awt.Dimension(60, 20));
        tfFornecedor.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                tfFornecedorFocusLost(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 5);
        jPanel6.add(tfFornecedor, gridBagConstraints);

        tfNomeFornecedor.setEditable(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 15);
        jPanel6.add(tfNomeFornecedor, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        add(jPanel6, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents

    private void tfImpostoFocusLost(java.awt.event.FocusEvent evt)//GEN-FIRST:event_tfImpostoFocusLost
    {//GEN-HEADEREND:event_tfImpostoFocusLost
        /**try
        {
            Imposto i = ImpostoManager.getInstance().getPessoa( Integer.parseInt( tfCadastro.getText() ) );
            if (p != null) 
            {
                tfNomeCadastro.setText( p.getNome() );
            }
        }
        catch ( Exception e ) { }*/
    }//GEN-LAST:event_tfImpostoFocusLost

    private void tfFornecedorFocusLost(java.awt.event.FocusEvent evt)//GEN-FIRST:event_tfFornecedorFocusLost
    {//GEN-HEADEREND:event_tfFornecedorFocusLost
        try
        {
            Pessoa p = PessoaManager.getInstance().getPessoa( Integer.parseInt( tfFornecedor.getText() ) );
            if (p != null) 
            {
                tfNomeFornecedor.setText( p.getNome() );
            }
        }
        catch ( Exception e ) { }
        
        // TODO add your handling code here:
    }//GEN-LAST:event_tfFornecedorFocusLost

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox cbInativo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JTextField tfAlternativo;
    private javax.swing.JTextField tfCodigo;
    private javax.swing.JTextField tfDescImposto;
    private javax.swing.JTextField tfDescMensagem;
    private javax.swing.JTextField tfDescricao;
    private javax.swing.JTextField tfEstruturado;
    private javax.swing.JTextField tfFornecedor;
    private javax.swing.JTextField tfImposto;
    private javax.swing.JTextField tfMensagem;
    private javax.swing.JTextField tfNcm;
    private javax.swing.JTextField tfNomeFornecedor;
    // End of variables declaration//GEN-END:variables
}
